//-----------------------------------------------------------------------
// <copyright file="UserOfficeDAOBase.cs" company="Pyramid Consulting">
//     Copyright (c) Pyramid Consulting. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Collections;
using MyFramework;
using MyFramework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using MIMS.WRS.Entities;

namespace MIMS.WRS.Data
{    
    public abstract class UserOfficeDAOBase
    {
        #region Common methods
        public virtual UserOffice CreateUserOfficeFromReader(IDataReader reader)
        {
            UserOffice item = new UserOffice();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("OfficeID"))) item.OfficeID = (int)reader["OfficeID"];
                if (!reader.IsDBNull(reader.GetOrdinal("UserID"))) item.UserID = (int)reader["UserID"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateUserOfficeFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreateUserOffice methods
            
        public virtual void CreateUserOffice(UserOffice userOffice)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spUserOfficesInsert");
                
                dbCommandWrapper.AddInParameter("@OfficeID", DbType.Int32, userOffice.OfficeID);
                dbCommandWrapper.AddInParameter("@UserID", DbType.Int32, userOffice.UserID);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateUserOfficeException, ex);
            }
        }

        #endregion

        #region UpdateUserOffice methods
        
        public virtual void UpdateUserOffice(UserOffice userOffice)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spUserOfficesUpdate");            
                
                dbCommandWrapper.AddInParameter("@OfficeID", DbType.Int32, userOffice.OfficeID);
                dbCommandWrapper.AddInParameter("@UserID", DbType.Int32, userOffice.UserID);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdateUserOfficeException, ex);
            }
        }
        
        #endregion

        #region DeleteUserOffice methods
        public virtual void DeleteUserOffice(int officeID, int userID)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spUserOfficesDelete");
                
                dbCommandWrapper.AddInParameter("@OfficeID", DbType.Int32, officeID);
                dbCommandWrapper.AddInParameter("@UserID", DbType.Int32, userID);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeleteUserOfficeException, ex);
            }
        }

        #endregion

        #region GetUserOffice methods
        
        public virtual UserOffice GetUserOffice(int officeID, int userID)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spUserOfficesGet");
                
                dbCommandWrapper.AddInParameter("@OfficeID", DbType.Int32, officeID);
                dbCommandWrapper.AddInParameter("@UserID", DbType.Int32, userID);
                
                UserOffice userOffice =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommandWrapper))
                {
                    if (reader.Read())
                    {
                        userOffice = CreateUserOfficeFromReader(reader);
                        reader.Close();
                    }
                }
                return userOffice;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetUserOfficeException, ex);
            }
        }
        
        #endregion

        #region GetUserOffices methods
        public virtual UserOfficeCollection GetUserOffices(UserOfficeColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spUserOfficesGetList");
                
                dbCommandWrapper.AddInParameter("@OrderBy", DbType.AnsiString, orderBy.ToString());
                dbCommandWrapper.AddInParameter("@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                dbCommandWrapper.AddInParameter("@Page", DbType.Int32, page);
                dbCommandWrapper.AddInParameter("@PageSize", DbType.Int32, pageSize);
                dbCommandWrapper.AddOutParameter("@TotalRecords", DbType.Int32, 4);
                
                UserOfficeCollection userOfficeCollection = new UserOfficeCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommandWrapper))
                {
                    while (reader.Read())
                    {
                        UserOffice userOffice = CreateUserOfficeFromReader(reader);
                        userOfficeCollection.Add(userOffice);
                    }
                    reader.Close();
                }
                totalRecords = (int)dbCommandWrapper.GetParameterValue("@TotalRecords");
                return userOfficeCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetUserOfficesException, ex);
            }
        }
        
        public virtual UserOfficeCollection GetUserOffices(UserOfficeColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetUserOffices(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
    }
}